package leetcode;

import java.util.ArrayList;
import java.util.List;

public class Leetcode77 {
    List<List<Integer>> res;
    List<Integer> list;
    public List<List<Integer>> combine(int n, int k) {
        this.res = new ArrayList<List<Integer>>();
        this.list = new ArrayList<Integer>();
        dfs(n,k,1);
        return res;
    }

    public void dfs(int n, int k,int index){
        if(k==0){//组合的个数够了，返回
            res.add(new ArrayList(list));
            return;
        }
        for(int i = index;i<=n;i++){
            if(k>n-i+1){//后面数字不够选剪枝
                continue;
            }
            list.add(i);
            dfs(n,k-1,i+1);
            list.remove(list.size()-1);
        }
    }
}
